package com.example.modelsystem.controller;

import com.example.modelsystem.dao.SysUserMapper;
import com.example.modelsystem.entity.SysUserEntity;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录接口
 *
 * @author : bugu
 * @date : 2024-04-28 09:30
 */
@Api(tags = "登录页面")
@Scope("singleton")
@RestController
public class LoginController {

    @Autowired
    SysUserMapper sysUserMapper;

    @ApiOperation("新注册一个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "username", value = "用户名", defaultValue = "张三"),
            @ApiImplicitParam(paramType = "query", required = true, name = "password", value = "密码", defaultValue = "123456")})
    @ApiResponses({
            @ApiResponse(code = 422, message = "用户名或密码不能为空"),
            @ApiResponse(code = 409, message = "用户名已存在"),
            @ApiResponse(code = 201, message = "注册成功", response = SysUserEntity.class)
    })
    @PostMapping("/register")
    public ResponseEntity register(@RequestParam String username, @RequestParam String password) {
        if (username == null || "".equals(username.trim()) || password == null || "".equals(password.trim())) {
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("用户名或密码不能为空");
        }
        SysUserEntity byUsername = sysUserMapper.findByUsername(username);
        if (byUsername != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("用户名已存在");
        }
        SysUserEntity newUser = SysUserEntity.builder()
                .username(username)
                .password(password)
                .build();
        sysUserMapper.insertUser(newUser);
        return ResponseEntity.status(HttpStatus.CREATED).body(newUser);

    }

    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "username", value = "用户名", defaultValue = "张三"),
            @ApiImplicitParam(paramType = "query", required = true, name = "password", value = "密码", defaultValue = "123456")})
    @ApiResponses({
            @ApiResponse(code = 422, message = "用户名或密码不能为空"),
            @ApiResponse(code = 403, message = "请先注册后再登录"),
            @ApiResponse(code = 401, message = "密码错误"),
            @ApiResponse(code = 200, message = "登录成功", response = SysUserEntity.class)
    })
    @PostMapping("/login")
    public ResponseEntity login(@RequestParam String username, @RequestParam String password) {
        if (username == null || "".equals(username.trim()) || password == null || "".equals(password.trim())) {
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("用户名或密码不能为空");
        }
        SysUserEntity byUsername = sysUserMapper.findByUsername(username);
        if (byUsername == null) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("请先注册后再登录");
        }

        String _password = byUsername.getPassword();
        if (_password != null && _password.equals(password)) {
            return ResponseEntity.ok(byUsername);
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密码错误");
    }

    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "username", value = "用户名", defaultValue = "张三"),
            @ApiImplicitParam(paramType = "query", required = true, name = "password", value = "密码", defaultValue = "123456")})
    @ApiResponses({
            @ApiResponse(code = 422, message = "用户名或密码不能为空"),
            @ApiResponse(code = 403, message = "用户不存在"),
            @ApiResponse(code = 202, message = "更新成功")
    })
    @PutMapping("/reset-password")
    public ResponseEntity resetPassword(@RequestParam String username, @RequestParam String password) {
        if (username == null || "".equals(username.trim()) || password == null || "".equals(password.trim())) {
            return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("用户名或密码不能为空");
        }
        Integer affectRows = sysUserMapper.updateByUsername(username, password);
        if (affectRows == null || affectRows == 0) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("用户不存在");
        } else {
            return ResponseEntity.status(HttpStatus.ACCEPTED).body("更新成功");
        }
    }
}
